/**
 * created by 小卷毛, 2020-05-22 022
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.aifeng.view

import com.aifeng.utils.applyObservableSchedulers
import com.kongqw.wechathelper.net.IBaseRequest
import com.kongqw.wechathelper.net.response.AccessTokenInfo
import com.kongqw.wechathelper.net.response.WeChatUserInfo
import com.lzg.extend.JsonConvert
import com.lzg.okrx.adapter.ObservableBody
import com.lzy.okgo.OkGo
import io.reactivex.Observable

/**
 * 项目名称：Hassan_Food
 * 创建人：小卷毛
 * 创建时间：2020-05-22 12:45
 */
class WXRequest : IBaseRequest {

    override fun getAccessToken(
        appId: String,
        secret: String,
        code: String
    ): Observable<AccessTokenInfo> =
        OkGo.get<AccessTokenInfo>("https://api.weixin.qq.com/sns/oauth2/access_token")
            .params("appid", appId)
            .params("secret", secret)
            .params("code", code)
            .params("grant_type", "authorization_code")
            .converter(object : JsonConvert<AccessTokenInfo>() {})
            .adapt(ObservableBody())
            .compose(applyObservableSchedulers())

    override fun getWeChatUserInfo(
        accessToken: String,
        openId: String
    ): Observable<WeChatUserInfo> =
        OkGo.get<WeChatUserInfo>("https://api.weixin.qq.com/sns/userinfo")
            .params("access_token", accessToken)
            .params("openid", openId)
            .converter(object : JsonConvert<WeChatUserInfo>() {})
            .adapt(ObservableBody())
            .compose(applyObservableSchedulers())

}